//MapView Component Constructor
function MapView() {
	
	//create view instance
	var self = Ti.Map.createView({
		mapType: Titanium.Map.STANDARD_TYPE,
    	region: {latitude:43.046515, longitude:-87.907718, 
            	latitudeDelta:0.01, longitudeDelta:0.01},
   		animate:true,
    	regionFit:true,
    	userLocation:true
	});
	
	//variable to hold yaw
	var yaw = 0;
	
	//local listener for touch events
	self.addEventListener('click', function(evt) {
    	Ti.API.info("Annotation " + evt.title + " clicked, id: " + evt.annotation.myid);

    	// Check for all of the possible names that clicksouce
    	// can report for the left button/view.
    	if (evt.clicksource == 'leftButton' || evt.clicksource == 'leftPane' || evt.clicksource == 'leftView') {
        	Ti.API.info("Annotation " + evt.title + ", left button clicked.");
   		}
	});
	
	//global listener for gps messages
	Ti.App.addEventListener('newGPSdata', function(evt) {		
		Titanium.API.log("debug","new gps data event recieved");
		latDelta = self.getLatitudeDelta();
		longDelta = self.getLatitudeDelta();
	  	self.setLocation({latitude: evt.latitude, longitude: evt.longitude, 
	  		latitudeDelta: latDelta, longitudeDelta: longDelta, animate: true	
	  	});
	  	
	  	//create the annotation that represents the vehicle
		copterAnn = Titanium.Map.createAnnotation({
    		latitude:evt.latitude,
    		longitude:evt.longitude,
    		title:"Copter Current Position",
    		animate:false,
    		image:'/ui/common/images/quad.png',
    		myid:1 // Custom property to uniquely identify this annotation.
		});
		
		//assign annotation to the map
		ann = new Array();
		ann[0] = copterAnn;
		self.removeAllAnnotations();
		self.annotations = ann;
		
		Titanium.API.log("debug", evt.latitude);
	});
	
	//get the yaw from the hud data
	Ti.App.addEventListener('newHUDdata', function(evt) {		
		yaw = evt.yaw;
	});
	
	//start the gpsActivity in the comms module
	Ti.App.fireEvent('startGPS', null);
	
	return self;
}

//make constructor function the public component interface
module.exports = MapView;

